# -*- coding: utf-8 -*-
#
# The Philatelist program is a fork of the OpenNumismat program
# Copyright (c) 2011 - 2024 Vitaly Ignatov (OpenNumismat) <opennumismat@gmail.com>
# Copyright (c) 2021 - 2024 Victor Nabatov (Philatelist) <nabatov.victor@gmail.com>
#

from PySide6.QtCore import Qt, QEvent
from PySide6.QtGui import QKeyEvent
from PySide6.QtWidgets import QBoxLayout

from Philatelist.ImageView import ImageView
from Philatelist.DetailsView import DetailsView
from Philatelist.ListView import ListView, IconView
from Philatelist.StatisticsView import StatisticsView
from Philatelist.TagsView import TagsView
from Philatelist.TreeView import TreeView
from Philatelist.Collection.CollectionPages import CollectionPageTypes
from Philatelist.Tools.Gui import Splitter


class PageView(Splitter):

    def __init__(self, pageParam, parent=None):
        super().__init__('0', parent=parent)
        self.imagesAtBottom = pageParam.images_at_bottom
        self._model = None
        self.param = pageParam
        self.id = pageParam.id
        self.treeView = TreeView(pageParam.treeParam, self)
        self.tagsView = TagsView(self)
        if self.param.type == CollectionPageTypes.Icon:
            self.listView = IconView(self.param.listParam, self)
        else:
            self.listView = ListView(self.param.listParam, self)

        if self.imagesAtBottom:
            self.imageView = ImageView(QBoxLayout.LeftToRight, self)
            self.detailsView = DetailsView(QBoxLayout.TopToBottom, self)
        else:
            self.imageView = ImageView(QBoxLayout.TopToBottom, self)
            self.detailsView = DetailsView(QBoxLayout.LeftToRight, self)

        self.imageView.prevRecordEvent.connect(self.prevRecord)
        self.imageView.nextRecordEvent.connect(self.nextRecord)

        self.splitter1 = Splitter('1', Qt.Vertical, self)
        splitter2 = Splitter('2', parent=self.splitter1)
        splitter3 = Splitter('3', Qt.Vertical, parent=splitter2)
        splitter3.addWidget(self.treeView)
        splitter3.addWidget(self.tagsView)
        splitter2.addWidget(splitter3)
        splitter2.addWidget(self.listView)
        self.splitter1.addWidget(splitter2)
        if self.imagesAtBottom:
            self.splitter1.addWidget(self.imageView)
        else:
            self.splitter1.addWidget(self.detailsView)

        self.statisticsView = StatisticsView(pageParam.statisticsParam)
        self.statisticsView.setMinimumHeight(200)

        self.addWidget(self.splitter1)
        if self.imagesAtBottom:
            self.addWidget(self.detailsView)
        else:
            self.addWidget(self.imageView)

        self.splitterMoved.connect(self.splitterPosChanged)

    def setModel(self, model, reference):
        self._model = model
        self.treeView.setModel(model, reference)
        self.tagsView.setModel(model)
        self.listView.setModel(model)
        self.imageView.setModel(model)
        self.detailsView.setModel(model)
        self.statisticsView.setModel(model)
        self.prepareInfo()

        self.listView.rowChanged.connect(self.imageView.rowChangedEvent)
        self.listView.rowChanged.connect(self.treeView.rowChangedEvent)
        self.listView.rowChanged.connect(self.detailsView.rowChangedEvent)
        if model.settings['tags_used']:
            self.treeView.currentItemChanged.connect(self.tagsView.clearSelection)
            self.tagsView.currentItemChanged.connect(self.treeView.clearSelection)
            self._model.tagsChanged.connect(self.tagsView.tagsChanged)

        self._model.modelChanged.connect(self.modelChanged)
        if not model.settings['tags_used']:
            self.tagsView.hide()

    def model(self):
        return self._model

    def modelChanged(self):
        self.treeView.modelChanged()
        self.listView.modelChanged()
        if self.param.info_type == CollectionPageTypes.Statistics:
            self.statisticsView.modelChanged()

    def prepareInfo(self):
        sizes = self.splitter1.sizes()
        if self.param.info_type == CollectionPageTypes.Statistics:
            self.splitter1.replaceWidget(1, self.statisticsView)
        else:
            if self.imagesAtBottom:
                self.splitter1.replaceWidget(1, self.imageView)
            else:
                self.splitter1.replaceWidget(1, self.detailsView)

        if sizes[1] > 0:
            self.splitter1.setSizes(sizes)

    def showInfo(self, info_type):
        self.param.info_type = info_type
        self.prepareInfo()
        if self.param.info_type == CollectionPageTypes.Statistics:
            self.statisticsView.modelChanged()

    def changeView(self, type_):
        index = self.listView.currentIndex()
        self.listView.rowChanged.disconnect(self.imageView.rowChangedEvent)
        self.listView.rowChanged.disconnect(self.treeView.rowChangedEvent)
        self.listView.rowChanged.disconnect(self.detailsView.rowChangedEvent)

        self.param.type = type_
        if self.param.type == CollectionPageTypes.Icon:
            listView = IconView(self.param.listParam, self)
            self._model.setFilter('')
        else:
            listView = ListView(self.param.listParam, self)

        splitter2 = self.splitter1.widget(0)
        old_widget = splitter2.replaceWidget(1, listView)
        old_widget.deleteLater()

        self.listView = listView
        self.listView.rowChanged.connect(self.imageView.rowChangedEvent)
        self.listView.rowChanged.connect(self.treeView.rowChangedEvent)
        self.listView.rowChanged.connect(self.detailsView.rowChangedEvent)
        self.listView.setModel(self._model)
        self.listView.modelChanged()

        if index.isValid():
            self.listView.setFocus()
            self.listView.scrollToIndex(index)

    def setCurrentStamp(self, stamp_id):
        self.listView.selectedId = stamp_id
        self.listView.modelChanged()

    def prevRecord(self, editor):
        event = QKeyEvent(QEvent.KeyPress, Qt.Key_Up, Qt.NoModifier, 0, 0, 0)
        self.listView.keyPressEvent(event)
        old_proxy = editor.proxy
        proxy = self.imageView.getImageProxy()
        proxy.setCurrent(old_proxy._current)
        editor.setImageProxy(proxy)
        old_proxy.deleteLater()

    def nextRecord(self, editor):
        event = QKeyEvent(QEvent.KeyPress, Qt.Key_Down, Qt.NoModifier, 0, 0, 0)
        self.listView.keyPressEvent(event)
        old_proxy = editor.proxy
        proxy = self.imageView.getImageProxy()
        proxy.setCurrent(old_proxy._current)
        editor.setImageProxy(proxy)
        old_proxy.deleteLater()
